import type { UserInfo } from '@vben/types';

import { baseRequestClient, requestClient } from '#/api/request';

export namespace AuthApi {
  /** 登录接口参数 */
  export interface LoginParams {
    password?: string;
    username?: string;
  }

  /** 登录接口返回值 */
  export interface LoginResult {
    access_token: string;
    refresh_token: string;
  }

  export interface RefreshTokenResult {
    access_token: string;
    refresh_token: string;
  }

  /**
   * 登录
   */
  export async function login(data: LoginParams) {
    return requestClient.post<LoginResult>('/auth/jwt/login', data);
  }

  /**
   * 刷新accessToken
   */
  export async function refreshToken(refreshToken: string) {
    return requestClient.post<RefreshTokenResult>('/auth/jwt/refresh', {
      params: {
        refresh_token: refreshToken,
      },
    });
  }

  /**
   * 退出登录
   */
  export async function logout() {
    return baseRequestClient.post('/auth/logout', {
      withCredentials: true,
    });
  }

  /**
   * 获取用户权限码
   */
  export async function getAccessCodes() {
    return requestClient.get<string[]>('/auth/codes');
  }

  /**
   * 获取用户信息
   */
  export async function getUserInfo() {
    return requestClient.get<UserInfo>('/auth/jwt/user_info', { params: { to_camel: true } });
  }
}
